package com.yongjie.springbootlogin.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**Druid配置
 * @author 13468
 */
@Configuration
public class DruidConfig {
    //绑定数据源配置
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }

    /**
     * 配置Druid监控
     * 1.配置一个监管后台的Servlet
     * 2.配置一个监控的filter
     */
    @Bean//1.配置一个监控后台的servlet
    public ServletRegistrationBean statViewServlet(){
        //StatViewServlet是配置后台的servlet
        ServletRegistrationBean<StatViewServlet> bean=
                new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
        //配置初始化参数
        Map<String,String>initParam=new HashMap<>();
        //访问的用户密码
        initParam.put(StatViewServlet.PARAM_NAME_USERNAME,"root");
        initParam.put(StatViewServlet.PARAM_NAME_PASSWORD,"root");
        //允许访问的ip,默认所有的ip访问
        initParam.put(StatViewServlet.PARAM_NAME_ALLOW,"");
        //禁止访问的ip
        initParam.put(StatViewServlet.PARAM_NAME_DENY,"");

        bean.setInitParameters(initParam);

        return bean;
    }
}
